﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Base;


public partial class Search_SearchNews : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            this.bt.Text = Common.GetBottom(bt.Text);
            this.lblHead.Text = Common.GetBottom(lblHead.Text);
	    this.main_right.Text = Common.GetBottom(this.main_right.Text);
        }
    }

    private void Bind()
    {
        string str = "";
        string type = "";
        int page = 1;
        if (Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "")
        {
            str = Base.GetRequest.Filter(Request.QueryString["keyword"].ToString());
            lblKey.Text += str;
        }
        if (Request.QueryString["page"] != null && Request.QueryString["page"] != "")
        {
            page = int.Parse(Request.QueryString["page"].ToString());
        }

        string strselect = "W_Consultative.*,W_ConsultativeType.Name";
        string table = "W_Consultative inner join W_ConsultativeType on W_Consultative.TypeID=W_ConsultativeType.ID";
        // string strWhere = "";//
        string strWhere = " Title like '%" + str + "%' ";
        //if (Request.QueryString["Type"] != null && Request.QueryString["Type"] != "")
        //{
        //    type = Request.QueryString["Type"].ToString();
        //    strWhere += "and  W_ConsultativeType.Name='%" + type + "%'";
        //}
        DataTable dt = PageList.GetDataTable(strWhere, table, 1, "AddDate", "*", 10, page, 0);
        DataTable dc = PageList.GetDataTable(strWhere, table);
        if (dt.Rows.Count == 0)
        {
            lblMessage.Visible = true;
            lblCount.Text = "0";
            lblMessage.Text = "<font color='red' size='5pt'>暂无数据</font>";
        }
        else
        {
            lblMessage.Visible = false;
            lblCount.Text = " " + dc.Rows[0][0].ToString() + " ";
        }
        dlstNews.DataSource = dt.DefaultView;
        dlstNews.DataBind();
        lblPagination.Text = PageList.GoToPager(page, 10, "SearchNews.aspx?page", strWhere, table);

    }

    public string GetShortDate(string dateStr)
    {
        if (dateStr != string.Empty)
            return Convert.ToDateTime(dateStr).ToShortDateString();
        else
            return String.Empty;
    }

    public string ProcessImg(string strImage)
    {
        if (strImage != string.Empty)
        {
            string image = ProcessAbsoluteUrl(strImage);
            string str = "<img src='" + image + "'width='83px' height='83px'>";
            return str;
        }
        else
            return string.Empty;
    }

    protected void dropArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = "";
        int page = 1;
        string Date = dropDate.SelectedValue;
        if (Request.QueryString["str"] != null && Request.QueryString["str"] != "")
        {
            str = Request.QueryString["str"].ToString();
            str = Base.GetRequest.Filter(str);
        }
        if (Request.QueryString["page"] != null && Request.QueryString["page"] != "")
        {
            page = int.Parse(Request.QueryString["page"].ToString());
            bool strPage = Base.GetRequest.isNum(page.ToString());
            if (!strPage)
            {
                lblMessage.Text = "<font color='red' size='5pt'>页码格式不正确</font>";
                return;
            }            
        }
        string strWhere = "";                        //1=1 and name like '%'" + str + "'%'
        string strselect = "W_Consultative.*,W_ConsultativeType.Name";
        string table = "W_Consultative inner join W_ConsultativeType on W_Consultative.TypeID=W_ConsultativeType.ID";
        if (Date != "")
        {
            switch (Date.ToString())
            {
                case "最近三天":
                    strWhere = strWhere + " datediff(day,AddDate,getdate())<=3";
                    break;
                case "最近一周":
                    strWhere = strWhere + " datediff(day,AddDate,getdate())<=7";
                    break;
                case "最近一个月":
                    strWhere = strWhere + " datediff(day,AddDate,getdate())<=30";
                    break;
                case "最近三个月":
                    strWhere = strWhere + " datediff(m,AddDate,getdate()) <=3";
                    break;
                default:
                    // strWhere = strWhere + " datediff(m,AddDate,getdate())";
                    break;
            }
        }
        DataTable dt = PageList.GetDataTable(strWhere, table, 1, "AddDate", "*", 10, page, 0);
        DataTable dc = PageList.GetDataTable(strWhere, table);
        if (dt.Rows.Count == 0)
        {
            lblMessage.Visible = true;
            lblCount.Text = "0";
            lblMessage.Text = "<font color='red' size='5pt'>暂无数据</font>";
        }
        else
        {
            lblMessage.Visible = false;
            lblCount.Text = " " + dc.Rows[0][0].ToString() + " ";
        }
        dlstNews.DataSource = dt.DefaultView;
        dlstNews.DataBind();

        lblPagination.Text = PageList.GoToPager(page, 10, "SearchNews.aspx?page", strWhere, table);
        dropDate.SelectedValue = Date;
    }
}
